/**
 Team: Dream-Lab
 Author: مۇختەرجان مەخمۇت (yeganaaa@163.com)
 Time: 2020/9/25 11:53 AM GMT+06:00
 **/

import { _decorator, Component, Node, Quat, EventKeyboard, systemEvent, SystemEvent, Vec3, AnimationComponent, CCFloat, Tween } from 'cc';
const { ccclass, property } = _decorator;

enum ActorDirection {
    UP = 0x01,
    RIGHT = 0x02,
    BOTTOM = 0x03,
    LEFT = 0x04,
}

@ccclass('Actor')
export class Actor extends Component {
    private _animationComponent: AnimationComponent;
    private get degress() {
        return 1 / 90
    }
    @property({type: CCFloat})
    public rotationAnimationSpeed: number = 0.3
    @property({type: Node})
    public cameraNode: Node = null
    @property({type: CCFloat})
    public moveSpeed: number = 1.0
    private direction: ActorDirection = ActorDirection.UP
    private _distanceBetweenSelfNodeAndCamera = new Vec3(0.0, 0.0, 0.0)
    private _hasActorStopped = true

    protected start() {
        this._animationComponent = this.getComponent(AnimationComponent) as AnimationComponent
        if (this._animationComponent == null) throw new Error("Cannot find animation component, please add an animation component and set necessary animation clips.")
        this._distanceBetweenSelfNodeAndCamera.x = this.node.position.x - this.cameraNode.position.x
        this._distanceBetweenSelfNodeAndCamera.z = this.node.position.z - this.cameraNode.position.z
        this._distanceBetweenSelfNodeAndCamera.y = this.cameraNode.position.y
    }
    public update(deltaTime: number) {
        if (this._hasActorStopped) return
        this.node.translate(new Vec3(0, 0, deltaTime * this.moveSpeed))
        this.cameraNode.setPosition(
            new Vec3(
                this.node.position.x - this._distanceBetweenSelfNodeAndCamera.x,
                this._distanceBetweenSelfNodeAndCamera.y,
                this.node.position.z - this._distanceBetweenSelfNodeAndCamera.z
            )
        )
    }
    public goRun() {
        this._onStartMoving()
        this._animationComponent.crossFade("GoOn", this.rotationAnimationSpeed)
    }
    public goBack() {
        this._onStartMoving()
        this._animationComponent.crossFade("GoBack", this.rotationAnimationSpeed)
    }
    public goRight() {
        // if (this.direction == ActorDirection.UP) {
        //     this.node.rotate(new Quat(0.0, 3))
        // }
        this._onStartMoving()
        this._animationComponent.crossFade("GoRight", this.rotationAnimationSpeed)
    }
    public goLeft() {
        this._onStartMoving()
        this._animationComponent.crossFade("GoLeft", this.rotationAnimationSpeed)
    }
    public stopMoving() {
        this._hasActorStopped = true
    }
    private _onStartMoving() {
        this._hasActorStopped = false
    }
    public rotationAnimationCompleted(direction: ActorDirection) {
        this.direction = direction
        if (this._hasActorStopped) return
    }
}
